package nd.android.am;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import nd.android.am.bean.AccountBean;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

public final class Adapter {
    public static final String AUTHORITY = "nd.android.am.Account";

    private Adapter() {}
    
    public static List<String> users;
    public static Map<String, String> configs;
    
    public static final class Account implements BaseColumns {

    	private Account() {}
    	
    	public static final int STATUS_NORMAL = 0;
    	public static final int STATUS_SETTLE = 1;	// >= 1
    	public static final int STATUS_DISCARD = -1;
    	public static final int STATUS_VALIDATE = -2;
    	

        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/accounts");
        public static final String CONTENT_TYPE = "nd.android.cursor.dir/nd.android.account";
        public static final String CONTENT_ITEM_TYPE = "nd.android.cursor.item/nd.android.account";
        
        //COLUMNS
        public static final String OCCUR_DATE = "occured";
        public static final String PURPOSE = "purpose";
        public static final String AMOUNT = "amount";
        public static final String PAYER = "payer";
        public static final String USERS = "users";
        public static final String DESC = "desc";
        public static final String UPDATE_DATE = "updated";
        public static final String STATUS = "status";
        
        public static final String[] PROJECTION = {
    		_ID, OCCUR_DATE, PURPOSE, AMOUNT, PAYER, USERS, DESC, UPDATE_DATE, STATUS
        };
        
        public static final String DEFAULT_SORT_ORDER = OCCUR_DATE + " DESC";
        
        /////////////////////////////////////////////////////////////

        public static AccountBean getAccount(Cursor cursor) {
        	if(cursor == null || cursor.getCount() == 0)
        		return null;
        	if(cursor.isAfterLast() || cursor.isBeforeFirst())
        		cursor.moveToFirst();
        	AccountBean acc = new AccountBean();
        	acc.setId(cursor.getLong(0));
        	acc.setOccurDate(cursor.getString(1));
        	acc.setPurpose(cursor.getString(2));
        	acc.setAmount(cursor.getLong(3) / 100.0d);
        	acc.setPayer(cursor.getString(4));
        	acc.setUsers(cursor.getString(5));
        	acc.setDesc(cursor.getString(6));
        	acc.setUpdateDate(cursor.getLong(7));
        	acc.setStatus(cursor.getInt(8));
        	return acc;
        }
    }
    
    public static final class User implements BaseColumns {

    	private User() {}

        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/users");
        public static final String CONTENT_TYPE = "nd.android.cursor.dir/nd.android.user";
        public static final String CONTENT_ITEM_TYPE = "nd.android.cursor.item/nd.android.user";

        //COLUMNS
        public static final String NAME = "name";
        
        public static final String[] PROJECTION = {
    		_ID, NAME
        } ;
        
        public static final String DEFAULT_SORT_ORDER = NAME + " ASC";
        
        /////////////////////////////////////////////////////////////
        
        public static List<String> getUsers(){
        	return users;
        }
        
        public boolean isExistUser(String name){
        	return users.contains(name);
        }
    }
    
    public static final class Config {
    	
    	private Config() {}
        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/configs");
        public static final String CONTENT_TYPE = "nd.android.cursor.dir/nd.android.config";
        public static final String CONTENT_ITEM_TYPE = "nd.android.cursor.item/nd.android.config";
    	
        public static final String NAME = "name";
        public static final String VALUE = "value";
        
        public static final String[] PROJECTION = {
    		NAME, VALUE
        } ;
        
        public static final String DEFAULT_SORT_ORDER = NAME + " ASC";
        
        /////////////////////////////////////////////////////////////
        
        public static String getConfig(String configName){
        	return configs.get(configName);
        }
    }
    
    public static void cacheConfigs(Cursor cursor){
    	if(configs != null)
    		return;
    	configs = new LinkedHashMap<String, String>();
    	if(cursor == null || cursor.getCount() == 0)
    		return;
    	cursor.moveToFirst();
    	while(!cursor.isAfterLast()){
    		String name = cursor.getString(0);
    		String value = cursor.getString(1);
    		configs.put(name, value);
    		cursor.moveToNext();
    	}
    }
    
    public static void cacheUsers(Cursor cursor) {
    	if(users != null)
    		return;
    	users = new ArrayList<String>();
    	if(cursor == null || cursor.getCount() == 0)
    		return;
    	cursor.moveToFirst();
    	while(!cursor.isAfterLast()){
    		String name = cursor.getString(1);
    		users.add(name);
    		cursor.moveToNext();
    	}
    }
}
